package test;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import library.DaoUtil;
import library.controller.BookController;
import library.controller.BorrowController;
import library.entity.Book;
import library.entity.BookingRecord;
import library.entity.Reader;

import org.junit.*;

public class UnitTestBooking {
	BorrowController borrow;
	BookController bookController;
	TestData testData;
	
	public class BorrowController_ExpireRecordTestStub extends BorrowController{
		// Class extend from borrow controller for test purpose 
		public BorrowController_ExpireRecordTestStub(Reader reader) throws SQLException {
			super(reader);
			// TODO Auto-generated constructor stub
		}
		
		public List<BookingRecord> getBookingRecord(){
			Calendar past_date = Calendar.getInstance();
			past_date.add(Calendar.MONTH, -1);
			List<BookingRecord> borrowRecordStub = new ArrayList<BookingRecord>();
			borrowRecordStub.add(new BookingRecord(bookController.getBookById(1), past_date, testData.getTestReader()));
			bookedRecord = borrowRecordStub;
			return bookedRecord;
		}
	}
	
	@Before
	public void init() throws SQLException{
		DaoUtil.init();
		testData = new TestData();
		borrow = new BorrowController(testData.getTestReader());
		bookController = new BookController();
	}
	
	@Test
	public void getReturnDate_Test(){
		Calendar date = borrow.calculateReturnDate();
		Calendar today = Calendar.getInstance();
		int dateDiff = date.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
		assertEquals(dateDiff, borrow.loanDate);
		assertTrue(date.after(today));
	}
	
	@Test
	public void getBook_Test(){
		Book book = bookController.getBookById(1);
		assertNotNull(book);
	}
	
	@Test
	public void checkExpireRecord_Test() throws SQLException{
		borrow = new BorrowController_ExpireRecordTestStub(testData.getTestReader());
		assertTrue(borrow.getExpiryRecord().size() > 0);
	}
}
